package com.shxc.smarttrade.old.utils;

import android.content.Context;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.MyApplication;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import okhttp3.Call;

import static com.shxc.smarttrade.MyApplication.getSystemCode;
import static com.shxc.smarttrade.MyApplication.getUniquePsuedoID;

/**
 * Created by jcdev1 on 2017/6/17.
 */

public class MyOkHttpUtils {

    private static String salt = "bcbfc6bca954050a87225706682cab0eb710b03d0f01759f026a3c32b961cdebb0b936e791a14077f9c4d548bfa8eb89de1b25cf72a5126c61255043719da8a9";

    public static void getServiceDatas(Context context,Map<String, Object> map, String url, final ServiceStatus serviceStatus) {
        //公共参数
        String version = MyApplication.verName;
        String os = "Android";
        String osVersion = getSystemCode();
        String uuid = getUniquePsuedoID();
        String rd = UUID.randomUUID().toString();
        Long t = System.currentTimeMillis();
        String sign = Md5.getSign3(uuid, rd, t.toString(), salt);

        Map<String, Object> mapPublic = new HashMap<>();
        mapPublic.put("version", version);
        mapPublic.put("os", os);
        mapPublic.put("channel", context.getResources().getString(R.string.channel));
        mapPublic.put("osVersion", osVersion);
        mapPublic.put("uuid", uuid);
        mapPublic.put("rd", rd);
        mapPublic.put("t", t + "");
        mapPublic.put("sign", sign);
        if (map == null || map.size() == 0)
            map = mapPublic;
        else
            map.putAll(mapPublic);

        OkHttpUtils
                .post()
                .url(url)
                .params(map)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        serviceStatus.faild(call, e, id);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Logger.e("DATAS", response);
                        serviceStatus.success(response, id);
                    }
                });
    }

    public static Object getMap(Context context) {
        String version = MyApplication.verName;
        String os = "Android";
        String osVersion = getSystemCode();
        String uuid = getUniquePsuedoID();
        String rd = UUID.randomUUID().toString();
        Long t = System.currentTimeMillis();
        String sign = Md5.getSign3(uuid, rd, t.toString(), salt);

        Map<String, String> mapPublic = new HashMap<>();
        mapPublic.put("version", version);
        mapPublic.put("channel", context.getResources().getString(R.string.channel));
        mapPublic.put("os", os);
        mapPublic.put("osVersion", osVersion);
        mapPublic.put("uuid", uuid);
        mapPublic.put("rd", rd);
        mapPublic.put("t", t + "");
        mapPublic.put("sign", sign);

        return mapPublic;
    }

    public interface ServiceStatus {

        void faild(Call call, Exception e, int id);

        void success(String response, int id);

    }
}
